package com.huabo.sql.record;

import com.huabo.sql.type.TypeHandlerRegistry;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * CommonModelContext class
 *
 * @author Jackie Liu
 * @date 2017/10/17
 */
public class DomainModelContext implements InitializingBean {

    protected static final Logger log = Logger.getLogger(DomainModelContext.class);

    //扩展的领域模型完整类名
    private List<String> extendClassList;
    //领域模型解析集合
    private Map<Class<?>, DomainModelAnalysis> modelMap = new HashMap<Class<?>, DomainModelAnalysis>();
    //数据库字段转换器
    private static final TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry();

    public DomainModelContext() {

    }

    public DomainModelAnalysis getDomainModelAnalysis(Class<?> clazz) {
        return modelMap.get(clazz);
    }

    public void addDomainModelAnalysis(DomainModelAnalysis domainModelAnalysis) {
        modelMap.put(domainModelAnalysis.getClazz(), domainModelAnalysis);
    }

    public List<String> getExtendClassList() {
        return extendClassList;
    }

    public void setExtendClassList(List<String> extendClassList) {
        this.extendClassList = extendClassList;
    }

    public static TypeHandlerRegistry getTypeHandlerRegistry() {
        return typeHandlerRegistry;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (extendClassList == null || extendClassList.isEmpty()) {
            return;
        }
        for (String className : extendClassList) {
            Class<?> clazz = Class.forName(className);

            DomainModelAnalysis domainModelAnalysis = new DomainModelAnalysis();
            domainModelAnalysis.setClazz(clazz);
            domainModelAnalysis.analysisBean();
            if (log.isDebugEnabled()) {
                log.debug(domainModelAnalysis);
            }
            modelMap.put(clazz, domainModelAnalysis);
        }

    }
}
